var Quad = Class.extend({
	init: function(context, materialName) {
		var gl = context.renderer.gl;
		this.material = context.materialManager.getMaterial(materialName);

		this.vertexBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
        var vertices = [
             1.0,  1.0,  0.0,
            -1.0,  1.0,  0.0,
             1.0, -1.0,  0.0,
            -1.0, -1.0,  0.0
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        this.vertexBuffer.itemSize = 3;
        this.vertexBuffer.numItems = 4;
		
		// Set the texture coordinates
		this.texCoordBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);
		var texCoords = [
			1.0, 0.0,
			0.0, 0.0,
			1.0, 1.0,
			0.0, 1.0
		];
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texCoords), gl.STATIC_DRAW);
		this.texCoordBuffer.itemSize = 2;
		this.texCoordBuffer.numItems = 4;

		this.modelMatrix = mat4.create();
        mat4.identity(this.modelMatrix);
	},

	
	onFrameUpdate: function(context, elapsedTime) {		
		if (true || this.material.texture.isLoaded()) {
			var gl = context.renderer.gl;
			var viewProjMatrix = context.renderer.viewProjMatrix;
	 
			// Bind the vertices for this pass
			this.material.bindBuffers(context, this.vertexBuffer, this.texCoordBuffer);
			
			// Set the uniforms
			var modelViewProjMatrix = mat4.create();
			mat4.multiply(viewProjMatrix, this.modelMatrix, modelViewProjMatrix);
			this.material.setModelViewProjMatrix(modelViewProjMatrix);
			this.material.applyUniforms(context);

			gl.drawArrays(gl.TRIANGLE_STRIP, 0, this.vertexBuffer.numItems);
		}
	},
	
	resetTransform: function() {
		mat4.identity(this.modelMatrix);
	},
	
	setPosition: function(position) {
        mat4.translate(this.modelMatrix, position);
	},
	
	setOrientation: function(angle, direction) {
		mat4.rotate(this.modelMatrix, degToRad(angle), direction);
	},
	
	setScale: function(scale) {
		mat4.scale(this.modelMatrix, scale);
	},
}); 
